package com.eascs.p2p.admin.manager.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.eascs.app.dao.PageResult;
import com.eascs.app.dao.SqlPageQuery;
import com.eascs.p2p.admin.manager.IInvestObjectManager;
import com.eascs.p2p.common.dao.BaseDao;
import com.eascs.p2p.common.entity.InvestObjectEntity;
import com.eascs.p2p.common.entity.LoaneeRefundPlanEntity;
import com.eascs.p2p.common.vo.InvestObjectVO;

import jodd.util.StringUtil;

@Repository
public class InvestObjectManagerImpl extends BaseDao implements IInvestObjectManager {

	@Override
	public PageResult<Object[]> findPageInvestObjectEntity(InvestObjectVO vo) {
		SqlPageQuery<Object[]> sqlPageQuery = new SqlPageQuery<>(Object[].class, vo.getPageSize(), vo.getShowPage());
		StringBuilder sql = new StringBuilder();
		if(vo.getType()==1){
		    sql.append("SELECT invest.id, bill_code,title, item2.item_name, loanee_name,loan_amount, concat(annualized_yield, '%') AS annualized_yield,  concat( CONVERT (deadline, CHAR), item.item_name ) AS deadline, create_time, CASE WHEN invest.object_status = 2 THEN '未发布' WHEN invest.object_status = 1 THEN '未审核' ELSE '已发布' END AS object_status FROM p2p_invest_object invest LEFT JOIN p2p_datadic_item item ON item.item_code = invest.deadline_type AND item.dd_no = 2 LEFT JOIN p2p_datadic_item item2 ON item2.item_code = invest.group_team AND item2.dd_no = 1 WHERE object_status IN (1, 2)");
		}else {
			sql.append("SELECT invest.id, bill_code,title, item2.item_name, loanee_name,loan_amount, concat(annualized_yield, '%') AS annualized_yield, concat( CONVERT (deadline, CHAR), item.item_name ) AS deadline, failure_time, concat( FORMAT( invested_amount / loan_amount * 100, 2 ), '%' ) AS invested_amount, CASE WHEN invest.object_status = 3 THEN '融资中' WHEN invest.object_status = 4 THEN '已满标' WHEN invest.object_status = 5 THEN '还款中' WHEN invest.object_status = 6 THEN '已结清' WHEN invest.object_status = 7 THEN '还款异常' WHEN invest.object_status = 8 THEN '逾期结清' WHEN invest.object_status = 9 THEN '逾期结清' WHEN invest.object_status = 10 THEN '终止标的' END AS object_status FROM p2p_invest_object invest LEFT JOIN p2p_datadic_item item ON item.item_code = invest.deadline_type AND item.dd_no = 2 LEFT JOIN p2p_datadic_item item2 ON item2.item_code = invest.group_team AND item2.dd_no = 1 WHERE object_status NOT IN (1, 2)");
			
		}
		
		//项目编号
		if (StringUtil.isNotBlank(vo.getBillCode())) {
			sql.append(" and bill_code like ?");
			sqlPageQuery.addParam("%" + vo.getBillCode() + "%");
		}
		// 标题
		if (StringUtil.isNotBlank(vo.getTitle())) {
			sql.append(" and title like ?");
			sqlPageQuery.addParam("%" + vo.getTitle() + "%");
		}
		//借款人id
		if(StringUtil.isNotBlank(vo.getLoaneeId())){
			sql.append(" and loanee_id = ?");
			sqlPageQuery.addParam(  vo.getLoaneeId() );
		}
		
		// 借款人姓名
		if (StringUtil.isNotBlank(vo.getLoaneeName())) {
			sql.append(" and loanee_name like ?");
			sqlPageQuery.addParam("%" + vo.getLoaneeName() + "%");
		}
		
		
		// 创建时间
		if (StringUtil.isNotBlank(vo.getBeginCreateTime())) {

			sql.append(" and date_format(create_time,'%Y-%m-%d') >= ?");
			sqlPageQuery.addParam(vo.getBeginCreateTime());
		}
		if (StringUtil.isNotBlank(vo.getEndCreateTime())) {

			sql.append(" and date_format(create_time,'%Y-%m-%d') <= ?");
			sqlPageQuery.addParam(vo.getEndCreateTime());
		}
		
		//借款类型
		if (StringUtil.isNotBlank(vo.getGroup())) {
			sql.append(" and group_team = ?");
			sqlPageQuery.addParam(vo.getGroup());
		}
		
	 //标的状态
		if (StringUtil.isNotBlank(vo.getStatus())) {
			sql.append(" and object_status = ?");
			sqlPageQuery.addParam(vo.getStatus());
		}
		sql.append(" and is_del= 0 order by publisher_time desc,create_time desc ");
		sqlPageQuery.setQuerySql(sql.toString());
		PageResult<Object[]> page = this.findDataWithPage(sqlPageQuery);
		return page;
	}

	@Override
	public String addInvestObjectEntity(InvestObjectEntity investObjectEntity) {
		Date date = new Date();
		investObjectEntity.setCreateTime(date);
		investObjectEntity.setUpdateTime(date);
		boolean flag = this.insert(investObjectEntity);
		if (flag == true) {
			return investObjectEntity.getId();
		} else {
			return null;
		}
	}

	@Override
	public boolean updateInvestObjectEntity(InvestObjectEntity investObjectEntity) {

		Date date = new Date();
		investObjectEntity.setUpdateTime(date);
		return this.updateById(investObjectEntity);
	}

	@Override
	public boolean deleteInvestObjectEntity(String id) {
		String sql = " update p2p_invest_object set is_del=1 where id=?";
		return this.execute(sql, id);
	}

	@Override
	public PageResult<InvestObjectEntity> findFontPageInvestObjectEntity(InvestObjectVO vo) {
		SqlPageQuery<InvestObjectEntity> sqlPageQuery = new SqlPageQuery<>(InvestObjectEntity.class, vo.getPageSize(), vo.getShowPage());
		StringBuilder sql = new StringBuilder();
		 sql.append("SELECT * FROM p2p_invest_object where 1=1");
			sql.append(" and object_status in(3,4) and is_del= 0 order by object_status , publisher_time desc,annualized_yield desc");
			sqlPageQuery.setQuerySql(sql.toString());
			PageResult<InvestObjectEntity> page = this.findDataWithPage(sqlPageQuery);
			return page;
	}

	@Override
	public InvestObjectEntity findInvestObjectDetail(String id) {
		
		 return this.getById(InvestObjectEntity.class, id);
	}

	@Override
	public boolean verifyBid(String id,int status,String operator) {
		

		Date date = new Date();
	  
	
	
		String sql="" ;
		if(status==2){
		  sql=" update p2p_invest_object set object_status=?,verifier=? , verifier_time=? where id=? " ; //审核
		}else if(status==3){
			sql=" update p2p_invest_object set object_status=?,publisher=? , publisher_time=? where id=? " ; //发布
		}
		if(status==10){
			sql=" update p2p_invest_object set object_status=?,terminator=? , terminate_time=? where id=? " ;//终止标的
		}
		if(status==5){
			sql=" update p2p_invest_object set object_status=?,loaner=? , loan_time=? where id=? " ;//放款
		}
		if(status==6){
			sql=" update p2p_invest_object set object_status=?, update_time=? where id=?  " ;//6：还款
			return this.execute(sql,status,date,id);
		}
		return this.execute(sql,status,operator,date,id);
	}

	@Override
	public InvestObjectVO getNotVerifyNum(InvestObjectVO vo) {
		StringBuilder sql = new StringBuilder();
		List<Object> listParams = new ArrayList<Object>();
		if (vo.getType() ==1) {
			sql.append("select count(1) as totalNum,sum(loan_amount) loan_amount from p2p_invest_object where object_status in(1,2) ") ;
		}else{
			sql.append("select count(1) as totalNum,sum(loan_amount) loan_amount,sum(invested_amount) invested_amount from p2p_invest_object where object_status not in(1,2)") ;
		}
		//项目编号
		if (StringUtil.isNotBlank(vo.getBillCode())) {
			sql.append(" and bill_code like ?");
			listParams.add("%" + vo.getBillCode() + "%");
		}
		// 标题
		if (StringUtil.isNotBlank(vo.getTitle())) {
			sql.append(" and title like ?");
			listParams.add("%" + vo.getTitle() + "%");
		}
		//借款人id
		if(StringUtil.isNotBlank(vo.getLoaneeId())){
			sql.append(" and loanee_id = ?");
			listParams.add(  vo.getLoaneeId() );
		}
		
		// 借款人姓名
		if (StringUtil.isNotBlank(vo.getLoaneeName())) {
			sql.append(" and loanee_name like ?");
			listParams.add("%" + vo.getLoaneeName() + "%");
		}
		
		
		// 创建时间
		if (StringUtil.isNotBlank(vo.getBeginCreateTime())) {

			sql.append(" and date_format(create_time,'%Y-%m-%d') >= ?");
			listParams.add(vo.getBeginCreateTime());
		}
		if (StringUtil.isNotBlank(vo.getEndCreateTime())) {

			sql.append(" and date_format(create_time,'%Y-%m-%d') <= ?");
			listParams.add(vo.getEndCreateTime());
		}
		
		//借款类型
		if (StringUtil.isNotBlank(vo.getGroup())) {
			sql.append(" and group_team = ?");
			listParams.add(vo.getGroup());
		}
		
	 //标的状态
		if (StringUtil.isNotBlank(vo.getStatus())) {
			sql.append(" and object_status = ?");
			listParams.add(vo.getStatus());
		}
		return this.findEntity(sql.toString(), InvestObjectVO.class,listParams.toArray());
	}
	
	@Override
	public boolean addRefundPlan(LoaneeRefundPlanEntity loaneeRefundPlanEntity){
		return this.insert(loaneeRefundPlanEntity);
	}
}
